<script setup lang="ts">
import { ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { PhLockKey, PhArrowLeft } from '@phosphor-icons/vue';

const route = useRoute();
const router = useRouter();
const albumId = route.params.id as string;

const password = ref('');
const loading = ref(false);
const error = ref('');

const verifyPassword = async () => {
  if (!password.value) {
    error.value = '请输入密码';
    return;
  }

  try {
    loading.value = true;
    // TODO: 调用API验证密码
    // const result = await verifyAlbumPassword(albumId, password.value);

    // 模拟验证
    await new Promise(resolve => setTimeout(resolve, 1000));

    // 验证成功，跳转到相册详情
    router.push(`/albums/${albumId}`);
  } catch (err) {
    error.value = '密码错误，请重试';
    // 添加输入框抖动效果
    const input = document.querySelector('input');
    input?.classList.add('shake');
    setTimeout(() => {
      input?.classList.remove('shake');
    }, 600);
  } finally {
    loading.value = false;
  }
};

const goBack = () => {
  router.push('/albums');
};
</script>

<template>
  <div class="verify-page">
    <Transition name="card" appear>
      <div class="verify-card">
        <button class="back-button" @click="goBack">
          <PhArrowLeft :size="20" />
          <span>返回相册列表</span>
        </button>

        <div class="card-content">
          <Transition name="icon" appear>
            <div class="lock-icon">
              <PhLockKey :size="48" weight="fill" />
            </div>
          </Transition>

          <Transition name="fade-slide-up" appear>
            <div class="text-content">
              <h2>私密相册</h2>
              <p class="tip">请输入密码访问该相册</p>
            </div>
          </Transition>

          <Transition name="fade-slide-up" appear>
            <div class="input-group">
              <input
                v-model="password"
                type="password"
                placeholder="请输入密码"
                @keyup.enter="verifyPassword"
              >
              <Transition name="error">
                <p v-if="error" class="error-message">{{ error }}</p>
              </Transition>
            </div>
          </Transition>

          <Transition name="fade-slide-up" appear>
            <button
              class="verify-button"
              :class="{ loading }"
              :disabled="loading"
              @click="verifyPassword"
            >
              <span class="button-text">{{ loading ? '验证中...' : '确认' }}</span>
              <div v-if="loading" class="loading-spinner"></div>
            </button>
          </Transition>
        </div>
      </div>
    </Transition>
  </div>
</template>

<style scoped lang="less">
.verify-page {
  min-height: 100vh;
  background-color: #f8fafc;
  display: flex;
  align-items: center;
  justify-content: center;
}

.verify-card {
  background-color: #ffffff;
  padding: 40px;
  border-radius: 16px;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.08);
  width: 100%;
  max-width: 400px;
  position: relative;

  .back-button {
    position: absolute;
    top: 20px;
    left: 20px;
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 8px 12px;
    border: none;
    background: none;
    color: #666666;
    font-size: 14px;
    cursor: pointer;
    transition: color 0.3s ease;

    &:hover {
      color: #1a1a1a;
    }
  }

  .card-content {
    text-align: center;
    margin-top: 20px;

    .lock-icon {
      width: 80px;
      height: 80px;
      background-color: #f0f7ff;
      border-radius: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
      margin: 0 auto 24px;
      color: #3b82f6;
    }

    .text-content {
      margin-bottom: 32px;

      h2 {
        font-size: 24px;
        font-weight: 600;
        color: #1a1a1a;
        margin-bottom: 8px;
      }

      .tip {
        color: #666666;
      }
    }

    .input-group {
      margin-bottom: 24px;

      input {
        width: 100%;
        padding: 12px 16px;
        border: 2px solid #e5e7eb;
        border-radius: 8px;
        font-size: 16px;
        transition: all 0.3s ease;
        box-sizing: border-box;

        &:focus {
          border-color: #3b82f6;
          outline: none;
          box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
          transform: translateY(-2px);
        }

        &::placeholder {
          color: #9ca3af;
        }

        &.shake {
          animation: shake 0.6s cubic-bezier(0.36, 0.07, 0.19, 0.97) both;
        }
      }

      .error-message {
        color: #ef4444;
        font-size: 14px;
        margin-top: 8px;
        text-align: left;
      }
    }

    .verify-button {
      width: 100%;
      padding: 12px;
      background-color: #3b82f6;
      color: white;
      border: none;
      border-radius: 8px;
      font-size: 16px;
      font-weight: 500;
      cursor: pointer;
      transition: all 0.3s ease;
      position: relative;
      display: flex;
      align-items: center;
      justify-content: center;

      &:hover:not(:disabled) {
        background-color: #2563eb;
      }

      &:active:not(:disabled) {
        transform: scale(0.98);
      }

      &:disabled {
        background-color: #93c5fd;
        cursor: not-allowed;
      }

      .button-text {
        opacity: 1;
        transition: opacity 0.3s ease;
      }

      &.loading {
        .button-text {
          opacity: 0;
        }
      }

      .loading-spinner {
        position: absolute;
        width: 20px;
        height: 20px;
        border: 2px solid rgba(255, 255, 255, 0.3);
        border-radius: 50%;
        border-top-color: white;
        animation: spin 1s linear infinite;
      }
    }
  }
}

// 动画
.card-enter-active,
.card-leave-active {
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}

.card-enter-from,
.card-leave-to {
  opacity: 0;
  transform: scale(0.95);
}

.icon-enter-active {
  transition: all 0.6s cubic-bezier(0.34, 1.56, 0.64, 1);
  transition-delay: 0.2s;
}

.icon-enter-from {
  opacity: 0;
  transform: scale(0.5);
}

.fade-slide-up-enter-active {
  transition: all 0.6s cubic-bezier(0.4, 0, 0.2, 1);
  transition-delay: 0.4s;
}

.fade-slide-up-enter-from {
  opacity: 0;
  transform: translateY(20px);
}

.error-enter-active,
.error-leave-active {
  transition: all 0.3s ease;
}

.error-enter-from,
.error-leave-to {
  opacity: 0;
  transform: translateY(-10px);
}

@keyframes shake {
  10%, 90% {
    transform: translateX(-1px);
  }
  20%, 80% {
    transform: translateX(2px);
  }
  30%, 50%, 70% {
    transform: translateX(-4px);
  }
  40%, 60% {
    transform: translateX(4px);
  }
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

// 响应式设计
@media (max-width: 768px) {
  .verify-card {
    padding: 32px 24px;
    margin: 20px;

    .card-content {
      .lock-icon {
        width: 64px;
        height: 64px;
        margin-bottom: 20px;
      }

      .text-content {
        h2 {
          font-size: 20px;
        }

        .tip {
          font-size: 14px;
        }
      }
    }
  }
}
</style>
